import { defineStore } from 'pinia';
import { store } from '@/store';

export interface IListItem {
  id: number;
  title: string;
  disabled: boolean;
  served: boolean;
  updateDate: string;
  createDate: string;
  content: string;
}

export interface IMessageState {
  results: IListItem[];
  total: number;
}

export const useMessageStore = defineStore({
  id: 'app-message',
  state: (): IMessageState => ({
    results: [],
    total: 0,
  }),
  getters: {
    getMessage(): IListItem[] {
      return this.results;
    },
    getTotal(): number {
      return this.total;
    },
  },
  actions: {
    pushMessage(list: IListItem[]) {
      this.results = [...this.results, ...list];
      this.total++;
    },
    setMessage(list: IListItem[]) {
      this.results = list;
    },
    setTotal(total: number) {
      this.total = total;
    },
  },
});

// Need to be used outside the setup
export function useMessageStoreWidthOut() {
  return useMessageStore(store);
}
